package com.example.springbootdeom.contrlloer;

import com.example.springbootdeom.entity.*;
import com.example.springbootdeom.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ManageController {
    @Autowired
    private ManageService manageService;

    @GetMapping("test.action")
    public String hellow(){
        return "test";
    }

    //添加用户方法
    @PostMapping("adduser.action")
    public String addUser(String uname,String pwd,Character sex,String status,Integer age,String birthday,String name){
//        System.out.println("进入添加方法");
        User u=new User();
//        System.out.println("uname:"+uname+"pwd:"+pwd+"sex"+sex+"age:"+age+"status:"+status);
        u.setUname(uname);
        u.setPassword(pwd);
        if(sex=='1')
        {
            u.setSex('男');
        }
        else {
            u.setSex('女');
        }
        u.setStatus(status);
        u.setAge(age);
        u.setBirthday(birthday);
        u.setName(name);
//        System.out.println(u);
        if(manageService.addUser(u))
        {
            return "ok";
        }

        return "添加失败";
    }
    //删除用户
    @PostMapping("delectUser.action")
    public String delectUser(int user_id){
        System.out.println(user_id);
        if(manageService.deleteUser(user_id))
        {
            return  "ok";
        }
        return "error";
    }
    //查询全部用户
    @PostMapping("getUser.action")
    public List<User> getUser(){

        return manageService.getUser();
    }
    //获取全部学生
    @PostMapping("/getAllStudent.action")
    public List<Student> getStudent(){
        return manageService.getStudent();
    }
    //获取全部教师
    @PostMapping("/getAllTeacher.action")
    public List<Teacher> getTeacher() {
        return manageService.getTeacher();
    }
    //修改用户
    @PostMapping("modify.action")
    public String modify(int id,String uname,String pwd,Character sex,Integer age,String birthday,String name){
        User u=new User();
        u.setUname(uname);
        u.setPassword(pwd);
        if(sex=='1')
        {
            u.setSex('男');
        }
        else {
            u.setSex('女');
        }
        u.setAge(age);
        u.setId(id);
        u.setBirthday(birthday);
        u.setName(name);
        System.out.println(u);
        if(manageService.modify(u))
        {
            return "ok";
        }
        return "error";
    }
    //修改方法
    @PostMapping("/newmodify.action")
    public String modify(@RequestBody User user){

        if(user.getSex()=='1')
        {
            user.setSex('男');
        }
        else {
            user.setSex('女');
        }
        System.out.println(user);
        if(manageService.modify(user))
        {
            return "ok";
        }
        return "error";
    }
    //    //加入班级(需要传用户id 加入成功返回true)
        @PostMapping("addClass.action")
        public boolean addClass(int id){
        return false;
        }
        //查询所有班级
    @PostMapping("getAllClass.action")
        public List<Classs> getAllClass(){
        return manageService.getAllClass();
    }

        //查询全校所有的课程
    @PostMapping("/getAllCourse.action")
    public List<Course> getAllCourse(){
        return manageService.getAllCourse();
    }
    //修改用户密码
    @GetMapping("setPassword.action")
    public String setPassword(String password,Integer id,String oldPassword){
        System.out.println("进入修改密码");
        String getoldPassword=manageService.getOldPassword(id);
        System.out.println("查询数据库老密码"+getoldPassword);
        System.out.println("前端传来的验证密码"+oldPassword);
        if(getoldPassword.equals(oldPassword))
        {
            if(manageService.setPassword(password,id))
            {
                return "ok";
            }
            else{
                return "error";
            }

        }

        return "密码输入错误";
    }

    //判断是否登录
    @GetMapping("judgeLogin.action")
    public User judgeLogin(HttpServletRequest request){

        HttpSession session= request.getSession();
        User user=(User)session.getAttribute("user");
        if(user!=null)
        {
            return  user;

        }
        return null;
    }


}
